// Function to calculate Periodic Payments.

function PMT1(rate, per, nper, pv, fv){
	fv = parseFloat(fv);
	nper = parseFloat(nper);
	pv = parseFloat(pv);
	per = parseFloat(per);

	if (( per == 0 ) || ( nper == 0 )){
		return(0);
	}

	rate = (rate)/(per * 100);

	if ( rate == 0 ){ // Interest rate is 0
		pmt_value = - (fv + pv)/nper;
	}else{
		x = Math.pow(1 + rate,nper);
		pmt_value = (rate * (fv + x * pv))/(-1 + x);
	}

	//pmt_value = conv_number(pmt_value,2);

	return (pmt_value);
}

function PMT(rate, nper, pv, fv, type) {
	if (!fv) fv = 0;
	if (!type) type = 0;

	if (rate == 0) return -(pv + fv)/nper;
	
	var pvif = Math.pow(1 + rate, nper);
	var pmt = rate / (pvif - 1) * -(pv * pvif + fv);

	if (type == 1) {
		pmt /= (1 + rate);
	};

	return pmt;
};
 
function IPMT(pv, pmt, rate, per) {
	var tmp = Math.pow(1 + rate, per);
	return 0 - (pv * tmp * rate + pmt * (tmp - 1));
};


function PPMT(rate, per, nper, pv, fv, type) {
	if (per < 1 || (per >= nper + 1)) return null;
	var pmt = this.PMT(rate, nper, pv, fv, type);
	var ipmt = this.IPMT(pv, pmt, rate, per - 1);
	return pmt - ipmt;
}


/*
function conv_number(expr, decplaces){ // This function is from David Goodman's Javascript Bible.

	var str = "" + Math.round(expr * Math.pow(10,decplaces));

	while (str.length <= decplaces) {
		str = "0" + str;
	}

	var decpoint = str.length - decplaces;
	return (str.substring(0,decpoint) + "." + str.substring(decpoint,str.length));
}*/